#include<stdio.h>  
#include"mpi.h"  
#define SIZE 4  
  
int main(int argc, char *argv[]){  
        int totalNumTasks, rankID;  
  
        float sendBuf[SIZE][SIZE] = {  
                {1.0,   2.0,    3.0,    4.0},  
                {5.0,   6.0,    7.0,    8.0},  
                {9.0,   10.0,   11.0,   12.0},  
                {13.0,  14.0,   15.0,   16.0}  
        };  
  
        MPI_Init(&argc, &argv);  
        MPI_Comm_rank(MPI_COMM_WORLD, &rankID);  
        MPI_Comm_size(MPI_COMM_WORLD, &totalNumTasks);  
  
        float totalSum;  
        if(totalNumTasks == SIZE){  
                int source = 0;  
                int sendCount = SIZE;  
                int recvCount = SIZE;  
                float recvBuf[SIZE];  
                //scatter data from source process to all processes in MPI_COMM_WORLD  
                MPI_Scatter(sendBuf, sendCount, MPI_FLOAT,   
                            recvBuf, recvCount, MPI_FLOAT, source, MPI_COMM_WORLD);  
  
                float sumPerProcess = recvBuf[0] + recvBuf[1] + recvBuf[2] + recvBuf[3];  
                printf("my rankID = %d, receive Results: %f %f %f %f, totalOnMe = %f\n",   
                        rankID, recvBuf[0], recvBuf[1], recvBuf[2], recvBuf[3], sumPerProcess);  
  
                int count = 1;  
                int root = 0;  
                MPI_Reduce(&sumPerProcess, &totalSum, count, MPI_FLOAT, MPI_SUM, root, MPI_COMM_WORLD);  
                printf("Process %d is done.\n", rankID);  
                fflush(stdout);  
        }  
        else  
                printf("Please specify -n %d\n", SIZE);  
  
        MPI_Finalize();  
        if(rankID == 0) printf("totalSum = %f\n", totalSum);  
        return 0;  
}
